package com.kingint.web.listener.repair;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kingint.mbg.bas.entity.BasWorkOrderType;
import com.kingint.mbg.ser.entity.SerDepartmentRepair;
import com.kingint.mbg.ser.entity.SerDepartmentRepairMiddle;
import com.kingint.mbg.ser.entity.SerReceiveWorkMiddle;
import com.kingint.mbg.ser.entity.SerWorkOrder;
import com.kingint.mbg.ser.mapper.SerDepartmentRepairMapper;
import com.kingint.mbg.ser.mapper.SerDepartmentRepairMiddleMapper;
import com.kingint.mbg.ser.mapper.SerWorkOrderMapper;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.web.admin.serve.service.SerReceiveWorkMiddleService;
import com.kingint.web.portal.base.service.WorkOrderTypeService;
import com.kingint.web.sys.service.UmsAdminService;
import com.kingint.web.util.DateUtil;
import com.kingint.web.util.WorkType;
import com.kingint.web.util.spring.SpringContextHolder;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * 维修单用户接单监听器
 */
@Slf4j
public class RepairReceiveListener implements TaskListener {

    @Override
    public void notify(DelegateTask delegateTask) {
        SerWorkOrderMapper orderMapper = SpringContextHolder.getBean(SerWorkOrderMapper.class);
        String processInstanceId = delegateTask.getProcessInstanceId();//流程实例id
        String eventName = delegateTask.getEventName();//获取事件名称
        if (eventName.equals("create")) {
            String receiveStatus = delegateTask.getVariable("receiveStatus").toString();
            if (receiveStatus != null && receiveStatus.equals("1")) {//用户接取维修
                QueryWrapper<SerWorkOrder> wrapper = new QueryWrapper<>();
                wrapper.eq("process_instance_id", processInstanceId);
                SerWorkOrder workOrder = new SerWorkOrder();
                workOrder.setOrderStatus(2);//处理中
                workOrder.setStartTime(new Date());//开始时间
                orderMapper.update(workOrder, wrapper);//更新相关额状态
            }
        }
        if (eventName.equals("complete")) {//complete,delete
            String completeStatus = delegateTask.getVariable("completeStatus").toString();

            //接单中间表
            SerReceiveWorkMiddleService middleService = SpringContextHolder.getBean(SerReceiveWorkMiddleService.class);

            String assignee = delegateTask.getAssignee();
            UmsAdminService adminService = SpringContextHolder.getBean(UmsAdminService.class);//查询用户信息
            UmsMember adminByName = adminService.getAdminByName(assignee);
            RuntimeService runtimeService = SpringContextHolder.getBean(RuntimeService.class);
            ProcessInstance instance = runtimeService.createProcessInstanceQuery()
                    .processInstanceId(processInstanceId)
                    .singleResult();

            String businessKey = instance.getBusinessKey();//业务信息
            String[] s = businessKey.split("_");
            Integer integer = Integer.valueOf(s[2]);//workId

            if (completeStatus != null && !completeStatus.equals("1")) {//被驳回
                SerWorkOrder workOrder = new SerWorkOrder();
                workOrder.setId(integer);
                workOrder.setOrderStatus(4);//挂起
                orderMapper.updateById(workOrder);//更新相关额状态
                //同时修改维修人员接单的状态
                Integer id = workOrder.getId();

                //更新相关的中间表
                SerReceiveWorkMiddle workMiddle = new SerReceiveWorkMiddle();
                workMiddle.setWorkId(id);
                workMiddle.setStatus(1);
                workMiddle.setReceiveUserId(adminByName.getId());
                middleService.updateReceiveWorkInfo(workMiddle);

            } else if (completeStatus != null && completeStatus.equals("1")) {//维修人员提交
                //更新状态
                SerWorkOrder workOrder = new SerWorkOrder();
                workOrder.setId(integer);
                workOrder.setEndTime(new Date());//结束时间
                workOrder.setOrderStatus(3);//待验收
                orderMapper.updateById(workOrder);//更新相关额状态
            }
        }
    }
}
